﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace ChiantiPhoneListApp.Threading
{
    public class Semaphore
    {
        
        private int maxHandles= 0;
        private int currentHandles = 0;

        public int MaxHandles
        {
            get { return maxHandles; }
            set { maxHandles = value; }
        }

        public Semaphore() { }
        public Semaphore(int maxHandles) {
            this.maxHandles = maxHandles;
        
        }

        public void Grab()
        {
            if (currentHandles < maxHandles)
            {
                currentHandles++;
            }
            else
            {
                
                while (currentHandles >= maxHandles) {
                    Thread.Sleep(100);
                }
                 
                currentHandles++;
            }
        }

        public void Release()
        {
            currentHandles--;
        }

    }
}
